package FileIO;

import ParsingSys.Value;
import java.io.*;
import java.lang.SecurityException;
import java.util.*;

public class OutputManager {

	static private Formatter output; // object used to output text to file

	
  
  //append output of mode 2 operation
	/**
	 * @param file, existing output file where new output will be appended
	 * @param match, the match value between two supplied input 
	 */
	public static void createOutputMode2(String file, boolean match){
		String message=new String();
		if (match){
			message="The Sequence of Values SATISTIFIES the Finite Partial Order";
		}
		else message="The Sequence of Values DOES NOT SATISFIES the Finite Partial Order";
		
		try {
			FileWriter out = new FileWriter(file,true);
			BufferedWriter writer = new BufferedWriter(out);
			writer.write(message);
			writer.close();
		}
		catch (SecurityException securityException) {
			System.err.println("You do not have write access to this file.");
			System.exit(1);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
   
   
  // enable user to open file
	public static void creatFile(String file, ArrayList<Value> parsedValues) 
   {
		
	  if(parsedValues.size()==0)
		  System.out.println(" Error: The text file doesn't satisfy the constraints of a finite part order, a empty file is generated.");
	   
      try
      {
        output = new Formatter(file);
      } // end try
      catch ( SecurityException securityException )
      {
         System.err.println(
            "You do not have write access to this file." );
         System.exit( 1 );
      } // end catch
      catch ( FileNotFoundException filesNotFoundException )
      {
         System.err.println( "Error creating file." );
         System.exit( 1 );
      } // end catch
      
     
      try // output values to file
      { 
      // write new record
    	  output.format("Topological Sort For%n");
    	  for(Value v : parsedValues){           
	          int dependenciesValueSize = v.getDependencies().size();
    		  if( dependenciesValueSize > 0){
	        	  output.format( "%s: ", v.getName());
	                  
	              for(Value vDep : v.getDependencies()){                       
	            	  if(dependenciesValueSize > 1)
	            		  output.format( "%s, ", vDep.getName());
	                  else output.format( "%s%n", vDep.getName());
	                       --dependenciesValueSize;                       
	              }                                    
	          }
	      }
    	  output.format( "is%n%n");        
               
    	  for(Value p : parsedValues)    
    		  output.format( "%s%n", p.getName());                                                                      
      } // end try
      catch ( FormatterClosedException formatterClosedException )
      {
    	  System.err.println( "Error writing to file." );
          return;
      } // end catch
      
      
       if ( output != null )
         output.close();
     
   } // end method creatFile
} // end class
